package cn.com.bluemoon.daps.model.controller.api;


import cn.com.bluemoon.daps.api.model.RemoteDataModelTableIndexService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiModel;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.model.entity.DapDataModelSubordinateKey;
import cn.com.bluemoon.daps.model.service.DapDataModelSubordinateKeyService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = {"FeignClient服务-模型索引"})
@RequestMapping(DapsApiModel.API_MODEL_TABLE_INDEX)
@BmPermission(value = "", name = "服务注册-模型索引", ignore = true)
@RestController
@Slf4j
public class DapDataModelTableIndexApi implements RemoteDataModelTableIndexService {

    @Resource
    DapDataModelSubordinateKeyService indexService;

    @PostMapping("/updateIndex")
    @ApiOperation(value = "更新索引信息")
    public ResultBean<Boolean> updateById(@RequestParam("indexId") String id,
                                          @RequestBody DapDataModelSubordinateKey newT) {
        LambdaUpdateWrapper<DapDataModelSubordinateKey> updateWrapper = indexService.bmLambdaUpdate()
                .set(DapDataModelSubordinateKey::getKeyChinName, newT.getKeyChinName())
                .set(DapDataModelSubordinateKey::getKeyMethod, newT.getKeyMethod())
                .set(DapDataModelSubordinateKey::getKeyName, newT.getKeyName())
                .set(DapDataModelSubordinateKey::getKeyType, newT.getKeyType())
                .set(DapDataModelSubordinateKey::getFieldIds, newT.getFieldIds())
                .eq(DapDataModelSubordinateKey::getId, id);
        boolean status = indexService.update(updateWrapper);
        log.info("更新表表索引，{}, 情况：{}", newT.getId(), status);
        return ResultBean.ok(status);
    }

    @DeleteMapping("/delete")
    public ResultBean<Boolean> removeById(@RequestParam("indexId") String id) {
        boolean status = indexService.removeById(id);
        log.info("删除表索引，{}, 情况：{}", id, status);
        return ResultBean.ok(status);
    }

    @PostMapping("/saveIndex")
    @ApiOperation("保存新增索引信息")
    public ResultBean<String> saveIndex(@RequestBody DapDataModelSubordinateKey newT) {
        boolean status = indexService.save(newT);
        log.info("增加表索引，{}, 情况：{}", newT.getId(), status);
        return ResultBean.ok(newT.getId());
    }

}
